﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using simpleHotel.Client.UserControls;
using DevExpress.XtraEditors;
using simpleHotel.Client.HotelReference;

namespace simpleHotel.Client.Hotel
{
    public partial class ucRoomItem : BaseUserControl
    {
        public DateTime _updateDate;
        public DateTime UpdateDate 
        {
            get { return _updateDate; }
            set { 
                _updateDate = value;
                this.label1.Text = value.ToString("dd/MM/yyyy");
            }
        }

        private DataTable _categories;
        public DataTable Categories //columns Id, Name, 
        {
            get { return _categories; }
            set { 
                _categories = value;
                InitTableLayout();
            }
        }

        private int _hotelId;
        public int HotelId
        {
            get { return this._hotelId; }
            set { this._hotelId = value; }
        }


        public Button SaveButton
        {
            get { return this.btnSave; }
        }

        private List<ucItem> ItemList { get; set; }

        public ucRoomItem()
        {
            InitializeComponent();
            ItemList = new List<ucItem>();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void ucRoomItem_Load(object sender, EventArgs e)
        {

        }

        private void InitTableLayout() 
        { 
            this.panel1.AutoScroll = true;
            if (this._categories.Rows.Count > 0) 
            {
                foreach(var item in _categories.Rows.Cast<DataRow>())
                {
                    var uc = NewUcItem(item);
                    this.ItemList.Add(uc);
                    this.panel1.Controls.Add(uc);
                }

            }
        }

        private ucItem NewUcItem(DataRow row) 
        {
            var uc = new ucItem 
            {
                Caption = row["Name"].ToString(),
                Id = (int)row["Id"],
                Value = 0,
                Dock = DockStyle.Top
            };

            return uc;
        }

        private void btnSave_Click_1(object sender, EventArgs e)
        {
            Save();
        }

        private void Save() 
        {
            AvailableRoomDTO dto = new AvailableRoomDTO();
            dto.StartDate = this.UpdateDate;
            dto.HotelId = this.HotelId;
            if (this.ItemList != null && this.ItemList.Count > 0) {
                dto.Count = new int[ItemList.Count];
                dto.RoomCategoryId = new int[ItemList.Count];
                for(int i=0; i<ItemList.Count; i++) 
                {
                    dto.RoomCategoryId[i] = ItemList[i].Id;
                    dto.Count[i] = ItemList[i].Value;
                }
            }

            var service = new HotelService();
            var result = service.UpdateAvailableRooms(dto);
            if (result != string.Empty)
                MessageBox.Show("Lỗi xảy ra khi lưu: \r\n" + result, "Thông báo");
            else
                MessageBox.Show("Đã lưu thành công !", "Thông báo");
        }

    }
}
